﻿using WinClient.FaultService;
using WinClient.UserService;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IdentityModel.Tokens;
using System.Linq;
using System.ServiceModel;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using WcfInspectorLib;

namespace WinClient
{
    public partial class Form1 : Form
    {
        private SoapHeaderClientInspector accessTokenbehavior;

        private FaultServiceClient faultService;
        
        public Form1()
        {
            InitializeComponent();

            btnThrowFault1.Click += ThrowFault_Click;
            btnThrowFault2.Click += ThrowFault_Click;
            btnThrowFault3.Click += ThrowFault_Click;
            btnThrowFault4.Click += ThrowFault_Click;

            faultService = new FaultServiceClient();
            //给服务终结点添加AccessToken行为，使其自动带上SoapHeader
            accessTokenbehavior = new SoapHeaderClientInspector("AccessToken", "ywt.WcfTest", "");
            faultService.Endpoint.EndpointBehaviors.Add(accessTokenbehavior);
        }

        private void ThrowFault_Click(object sender, EventArgs e)
        {
            try
            {
                switch ((sender as Button).Name)
                {
                    case "btnThrowFault1":
                        accessTokenbehavior.Value = "ThrowFault1";
                        faultService.ThrowFault1();
                        break;
                    case "btnThrowFault2":
                        accessTokenbehavior.Value = "ThrowFault2";
                        faultService.ThrowFault2();
                        break;
                    case "btnThrowFault3":
                        accessTokenbehavior.Value = "ThrowFault3";
                        faultService.ThrowFault3();
                        break;
                    case "btnThrowFault4":
                        accessTokenbehavior.Value = "ThrowFault4";
                        faultService.ThrowFault4();
                        break;
                    default:
                        break;
                }
            }
            catch (FaultException<string> ex)
            {
                MsgBox.ShowError(ex.Detail);
            }
            catch (FaultException<FaultInfo> ex)
            {
                MsgBox.ShowError($"方法名称：{ex.Detail.Action}\r\n错误消息：{ex.Detail.Message}");
            }
            catch (FaultException ex)
            {
                MsgBox.ShowError(ex.Message);
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            
        }
    }
}
